package com.longshen.dubbo.api.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.longshen.dubbo.api.base.Contents;
import com.longshen.dubbo.api.mapper.AdminMenuMapper;
import com.longshen.dubbo.api.model.AdminMenu;
import com.longshen.dubbo.api.sequence.SequenceService;
import com.longshen.dubbo.api.service.AdminMenuService;
import com.longshen.dubbo.api.type.MenuType;

import tk.mybatis.mapper.entity.Example;

@Service
public class AdminMenuServiceImpl implements AdminMenuService {

	@Autowired
	private AdminMenuMapper mapper;

	@Autowired
	private SequenceService seq;

	@Override
	public AdminMenu add(AdminMenu in) {
		in.setId(seq.getId());
		//目录是对组件进行设置
		if(MenuType.CATALOG.equals(in.getMenuType())) {
			//根目录使用Layout
			if(Contents.MENU_ROOT.equals(in.getParentId())) {
				in.setComponent(Contents.MENU_ROOT_COMPONENT);
				
			}else {
				//其他目录routerview

				in.setComponent(Contents.MENU_CATALOG_COMPONENT);

			}
		}
		mapper.insert(in);
		return in;
	}

	@Override
	public void delById(String id) {
       mapper.deleteByPrimaryKey(id);
       AdminMenu  sub=new AdminMenu();
       sub.setParentId(id);
     List<AdminMenu> list= mapper.select(sub);
     if(list !=null) {
    	 for (AdminMenu adminMenu : list) {
    		 delById(adminMenu.getId());
		}
     }
	}

	@Override
	public AdminMenu findByID(String id) {
		// TODO Auto-generated method stub
		return mapper.selectByPrimaryKey(id);
	}

	@Override
	public List<AdminMenu> listAll() {
		Example  eample=new Example(AdminMenu.class);
		eample.orderBy("menuSort").orderBy("id");
		return mapper.selectByExample(eample);
	}

	@Override
	public List<AdminMenu> allDirectory() {
		Example example = new Example(AdminMenu.class);
		example.createCriteria().andEqualTo("menuType", "0");
		example.setOrderByClause("id");
		return mapper.selectByExample(example);
	}

	@Override
	public void modById(AdminMenu in) {
		mapper.updateByPrimaryKey(in);		
	}

}
